﻿using System.Collections.Generic;

namespace Algoritmia.SemiRings
{
    public interface ISemiRing<TItem, in TWeight>
    {
        TItem Zero { get; }
        TItem One { get; }
        TItem Plus(TItem left, TItem right);
        TItem Times(TItem left, TWeight right);
    }

    public interface ISemiRing<T> : ISemiRing<T,T>
    {
    }

    public static class SemiRingExtensions
    {
        public static T1 Sum<T1,T2>(this ISemiRing<T1,T2> semiRing, IEnumerable<T1> items)
        {
            T1 result = semiRing.Zero;
            foreach(var item in items)
            {
                result = semiRing.Plus(result, item);
            }
            return result;
        }
    }
}
